﻿using System.Web.Optimization;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Inovout.Web.Html
{
    public static class ContentExtensions
    {
        public static IHtmlString Style(this HtmlHelper html, string stylePath)
        {
            //HttpBrowserCapabilitiesBase browser = html.ViewContext.HttpContext.Request.Browser;
            //if (browser.Browser.ToUpper() == "IE" && browser.MajorVersion < 9
            //    && stylePath.IndexOf("csun", StringComparison.OrdinalIgnoreCase) > -1)
            //{
            //    //html.ViewContext.Writer.WriteLine(Script(html, "~/Scripts/PIE.js").ToHtmlString());
            //}

            ContentResource styleResource = ContentResource.StyleResources.SingleOrDefault(s => s.Uri.Equals(stylePath));
            if (styleResource == null)
            {
                TagBuilder styleTagBuilder = new TagBuilder("link");
                styleTagBuilder.Attributes.Add("href", UrlHelper.GenerateContentUrl(stylePath,html.ViewContext.HttpContext));
                styleTagBuilder.Attributes.Add("rel", "style");
                styleTagBuilder.Attributes.Add("type", "text/css");

                return MvcHtmlString.Create(styleTagBuilder.ToString(TagRenderMode.SelfClosing));
            }
            else if (!string.IsNullOrEmpty(styleResource.CdnUri))
            {
                TagBuilder styleTagBuilder = new TagBuilder("link");
                styleTagBuilder.Attributes.Add("href", UrlHelper.GenerateContentUrl(styleResource.CdnUri,html.ViewContext.HttpContext));
                styleTagBuilder.Attributes.Add("rel", "style");
                styleTagBuilder.Attributes.Add("type", "text/css");

                return new MvcHtmlString(styleTagBuilder.ToString(TagRenderMode.SelfClosing));
            }
            else
            {
                return Styles.Render(stylePath);
            }
        }

        public static IHtmlString Script(this HtmlHelper html, string scriptPath)
        {
            ContentResource scriptResource = ContentResource.ScriptResources.SingleOrDefault(s => s.Uri.Equals(scriptPath));
            if (scriptResource == null)
            {
                TagBuilder tagBuilder = new TagBuilder("script");
                tagBuilder.Attributes.Add("src", UrlHelper.GenerateContentUrl(scriptPath, html.ViewContext.HttpContext));

                return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.SelfClosing));
            }
            else if (!string.IsNullOrEmpty(scriptResource.CdnUri))
            {
                TagBuilder tagBuilder = new TagBuilder("script");
                tagBuilder.Attributes.Add("src", UrlHelper.GenerateContentUrl(scriptResource.CdnUri,html.ViewContext.HttpContext));

                return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.SelfClosing));
            }
            else
            {
                return Scripts.Render(scriptPath);
            }
        }

        private static readonly string defaultThemeName = "Classic";
        public static IHtmlString Theme(this HtmlHelper html, string themePath)
        {
            themePath = themePath.Insert(1, defaultThemeName + ".");
            return Style(html,themePath);
        }
 
    }
}